package com.whfc.wxmp.api.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.whfc.common.base.BaseController;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.dto.AppWorkTypeDTO;
import com.whfc.emp.entity.ClassMeeting;
import com.whfc.entity.dto.Apply.rep.*;
import com.whfc.entity.dto.Apply.req.ApprovalRecordReq;
import com.whfc.entity.dto.Apply.req.QueryPersonnelReq;
import com.whfc.entity.dto.Apply.req.SafetyTrainingExamReq;
import com.whfc.entity.dto.Apply.req.AppEntryApplicationDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.mach.entity.AppMach;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.wxmp.api.sys.service.WxApplyService;
import com.whfc.wxmp.config.AbstractBaseController;
import com.whfc.wxmp.dto.EntryApplicationDTO;
import com.whfc.wxmp.dto.EntryApplicationDetailRep;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author xjy
 * @date 2025-09-13
 * 人员申请 and 审批
 */
@RestController
@RequestMapping("/mp/api/apply")
public class WxApplyController extends BaseController {
    @Autowired
    private WxApplyService wxApplyService;
    /**
     * 人员申请方法
     * 该方法用于处理人员申请相关业务逻辑。
     *
     * @return 描述申请操作响应的字符串
     */
    @PostMapping("/work/personnel")
    public Result saveApplyPersonnel(@CurrentWxUser WxUser user, @RequestBody AppEntryApplicationDTO req) {
        String string = wxApplyService.saveApplyPersonnel(user, req);
        if(!Objects.equals(string,"提交成功")){
            return ResultUtil.failure("提交失败");
        }
        return ResultUtil.success();
    }

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @PostMapping("/work/uplodFile")
    public Result uplodFile(@RequestParam("file") MultipartFile file) {
        return ResultUtil.success(wxApplyService.uplodFile(file));
    }
    /**
     * 上传视频
     *
     * @param file
     * @return
     */
    @PostMapping("/work/uplodVideo")
    public Result uplodVideo(@RequestParam("file") MultipartFile file, ClassMeeting classMeeting) {
        String filePath = wxApplyService.uplodFile(file);
        classMeeting.setFilePath(filePath);
        wxApplyService.updateFilePath(classMeeting);
        return ResultUtil.success(filePath);
    }
    /**
     * 获取工种
     * @return
     */
    @GetMapping("/work/queryCategory")
    public Result queryCategory(@CurrentWxUser WxUser user) {
        logger.info("APPMPUA|{}",
                user.getId());
        return ResultUtil.success(wxApplyService.queryCategory(user.getId()));
    }
    /**
     * 根据人员姓名查找人员
     *
     * @param req
     * @return
     */
    @PostMapping("/work/queryPersonnel")
    public Result queryPersonnel(@RequestBody QueryPersonnelReq req) {
       List<SafetyTrainingExamReq> examReqList = wxApplyService.queryPersonnel(req);
        return ResultUtil.success(examReqList);
    }

    /**
     * 入场审核详情
     * @return
     */
    @GetMapping("/work/queryEntryDetails")
    public Result queryEntryDetails(@CurrentWxUser WxUser user, @RequestParam("id") Integer id) {
        System.out.println(JSONObject.toJSONString(user));
        EntryApplicationDetailRep institutions = wxApplyService.queryEntryDetails(user, id);
        return ResultUtil.success(institutions);
    }

    /**
     * 入场详情
     * @return
     */
    @GetMapping("/work/queryEntryInfo")
    public Result queryEntryInfo(@CurrentWxUser WxUser user, @RequestParam("id") Integer id) {
        System.out.println(JSONObject.toJSONString(user));
        AppEntryApplicationWxRep applicationRep = wxApplyService.queryEntryInfo(id);
        return ResultUtil.success(applicationRep);
    }


    /**
     * 获取机构
     * @return
     */
    @GetMapping("/work/queryInstitution")
    public Result queryInstitution(@CurrentWxUser WxUser user,Integer deptId) {
        System.out.println(JSONObject.toJSONString(user));
        List<InstitutionRep> institutions = wxApplyService.queryInstitution(user,deptId);
        return ResultUtil.success(institutions);
    }

    /**
     * 获取设备
     * @return
     */
    @GetMapping("/work/queryEquipmentList")
    public Result queryEquipmentList(@RequestParam("deptId" ) Integer deptId) {
        List<AppMach> infoReps = wxApplyService.queryEquipmentList(deptId);
        return ResultUtil.success(infoReps);
    }


    /**
     * 获取审核列表
     * @return
     */
    @GetMapping("/work/queryReviewList")
    public Result queryReviewList(@CurrentWxUser WxUser user, ApprovalRecordReq req) {
        return ResultUtil.success(wxApplyService.entryReview(user, req));
    }

    /**
     * 获取审批列表
     * @return
     */
    @GetMapping("/work/queryApprovalList")
    public Result queryApprovalList(@RequestParam("applicationId" ) Integer applicationId) {
        List<ApprovalInfoRep> infoReps = wxApplyService.queryApprovalList(applicationId);
        return ResultUtil.success(infoReps);
    }

    /**
     * 审核
     *
     * @param id
     * @return
     */
    @GetMapping("/work/entryReview")
    public Result applyEntryReview(@CurrentWxUser WxUser user,@RequestParam("approvalResult") Integer approvalResult, @RequestParam("id") Integer id) {
        String review = wxApplyService.applyEntryReview(user,approvalResult, id);
        if(!Objects.equals(review,"审核成功!")){
            return ResultUtil.failure("审核失败");
        }
        return ResultUtil.success();
    }
}
